import 'package:flutter/cupertino.dart';
import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_gold.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/address_model.dart';
import 'package:gold/model/gold_recover_result_model.dart';
import 'package:gold/model/gold_recover_shop_model.dart';
import 'package:gold/route/routes.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:gold/widgets/dialog/permission_dialog.dart';
import 'package:gold/widgets/dialog/pickup_time_dialog.dart';
import 'package:permission_handler/permission_handler.dart';

/// @description:
/// @author tangzhentao
/// @created at 19:22 2024/11/30


class GoldRecoverViewmodel extends Viewmodel {
  /// 克数
  final TextEditingController weightController = TextEditingController(text: '10');
  final FocusNode weightNode = FocusNode();

  late final double _price;
  /// 类型:1=定价后来料,2=来料后定价
  double get price => _price;

  String _method = 'offline';
  /// 回收方式:online=线上,offline=线下
  String get method => _method;
  set method(String value) {
    hideKeyboard();
    if (_method != value) {
      _method = value;
      weightController.text = '10';
      _setCanClick();
      _weight = 10;
      _shop = null;
      _address = null;
      _time = null;
      setSuccessChange();
    }
  }

  double _weight = 10;
  /// 重量，单位克
  double get weight => _weight;

  int _type = 1;
  /// 类型:1=定价后来料,2=来料后定价
  int get type => _type;
  set type(int value) {
    hideKeyboard();
    if (_type != value) {
      _type = value;
      setSuccessChange();
    }
  }

  GoldRecoverShopModel? _shop;
  /// 预约门店
  GoldRecoverShopModel? get shop => _shop;
  set shop(GoldRecoverShopModel? value) {
    if (_shop != value) {
      _shop = value;
      setSuccessChange();
    }
  }

  AddressModel? _address;
  /// 取件地址
  AddressModel? get address => _address;
  set address(AddressModel? value) {
    if (_address != value) {
      _address = value;
      setSuccessChange();
    }
  }

  String? _time;
  /// 预约到店时间/预约取件时间
  String? get time => _time;
  set time(String? value) {
    if (_time != value) {
      _time = value;
      setSuccessChange();
    }
  }

  bool _canClick = false;
  /// 是否可以提交
  bool get canClick => _canClick;

  GoldRecoverViewmodel(this._price);

  /// 重量变化
  void onWeightChanged(String value) {
    final number = double.tryParse(value)?? 0;
    print.call('价格 $price, $number');
    if (_weight != number) {
      _weight = number;
      _setCanClick();
      notifyListeners();
    }
  }

  /// 设置是否可以点击
  void _setCanClick() {
    if (_method == 'online') {
      _canClick = _weight > 0 && _address != null && _time != null;
    } else {
      _canClick = _weight > 0 && _shop != null && _time != null;
    }
  }

  /// 地址
  Future location() async {
    if (_method == 'online') {
      final result = await XfgNav.pushNamed<AddressModel>(Routes.myAddress, arguments: true);
      if (result != null && _address != result) {
        _address = result;
        _setCanClick();
        notifyListeners();
      }
    } else {
      final permissionResult = await showRequestPermissionDialog(permissions: [Permission.locationWhenInUse]);
      if (permissionResult) {
        final result = await XfgNav.pushNamed<String>(Routes.goldRecoverShopList);
        if (result != null && _shop != result) {
          _shop = result;
          _setCanClick();
          notifyListeners();
        }
      }
    }
  }

  /// 时间
  Future dateTime() async {
    final dateTime = await showPickupTimeDialog();
    if (dateTime != null) {
      _time = dateTime;
      _setCanClick();
      notifyListeners();
    }
  }

  /// 确认预约
  Future confirmReserve() async {
    final Map<String, dynamic> data = {'method': _method, 'type': _type.toString(), 'weight': _weight.toString(), 'price': _price.toString()};
    if (_method == 'online') {
      data['address_id'] = _address?.id;
      data['pickup_time'] = _time;
    } else {
      data['shop_id'] = _shop?.id;
      data['arrive_time'] = _time;
    }
    final result = await Http().request<GoldRecoverResultModel>(ApiGold.recycleCreate, data: data, loadingText: '预约中...');
    if (result.success) {
      if (result.data?.id != null) {
        XfgNav.pushReplacementNamed(Routes.goldRecoverSuccess, arguments: {'id': result.data!.id!, 'type': _method});
      } else {
        '订单ID异常'.error();
      }
    }
  }
}